import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { CookieService } from 'ngx-cookie-service';

@Injectable()
export class LoginGuard implements CanActivate {
    constructor(private router: Router, private http: HttpClient, private cookieService: CookieService) {
    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
        const token = this.cookieService.get('token');
        if (!!token) {
            return true;
        } else {
            this.router.navigate(['/login']); // 没有用户登录的话就跳到登录页面
        }
        return false;
    }
}
